package com.ug_project.adapters

import android.support.v4.view.PagerAdapter
import android.view.View
import android.view.ViewGroup
import com.ug_project.objects.isNotNull
import java.lang.Exception

/**
 * Created by Administrator on 3/10/2017.
 */
open class PagerAdapter(val views: Array<View>, val title_s: Array<String>? = null) : android.support.v4.view.PagerAdapter() {
    init {
        title_s.isNotNull {
            if (it.size < views.size) throw Exception("Title نىڭ مەزمۇنى چوقۇم برىلگەن View نىڭ سانىغا تەڭ بولىشى كىرەك")
        }
    }
    override fun isViewFromObject(view: View?, `object`: Any?): Boolean {
        return view == `object`
    }

    override fun getCount(): Int {
        return views.size
    }

    override fun destroyItem(container: ViewGroup?, position: Int, `object`: Any?) {
        container?.removeView(views[position])
    }

    override fun instantiateItem(container: ViewGroup?, position: Int): Any {
        container?.addView(views[position])
        return views[position]
    }

    override fun getItemPosition(`object`: Any?): Int {
        return super.getItemPosition(`object`)
    }

    override fun getPageTitle(position: Int): CharSequence {
        return title_s?.get(position) ?: ""
    }
}